package pers.ht.mbpo.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class ExcelUtil {

    private static ResourceBundle rb = ResourceBundle.getBundle("pers.ht.mbpo.util.DB-config", Locale.US);
    private static String SAVE_PATH = rb.getString("savePath");

//    public static void logFile(String content) {
//        try {
//            BufferedWriter writer = new BufferedWriter(new FileWriter(new File(SAVE_PATH + "/log.txt"), true));
//            writer.write("\n" + content);
//            writer.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static List<String[]> impExcel(int sheetNum, String filePath) {
        Workbook wb;
        Sheet sheet;
        Row row;
        List<String[]> list = new ArrayList<>();
        wb = getDocType(filePath);
        if (wb != null) {
            int sheetSize = wb.getNumberOfSheets();
            if (sheetNum < sheetSize) {
                //获取第一个sheet
                sheet = wb.getSheetAt(sheetNum);

                //获取最大行数
                int rowNum = sheet.getLastRowNum();
                //获取第一行
                row = sheet.getRow(0);

                if (row != null) {
                    //获取最大列数
                    int colNum = row.getPhysicalNumberOfCells();
                    for (int i = 1; i <= rowNum; i++) {
                        String[] data = new String[colNum];
                        row = sheet.getRow(i);
                        if (row != null && (String) getCellValue(row.getCell(0))!="") {
                            for (int j = 0; j < colNum; j++) {
                                data[j] = (String) getCellValue(row.getCell(j));
                            }
                        } else {
                           continue;
                        }
                        list.add(data);
                    }
                }
            }

        }
        return list;
    }

    //读取excel
    private static Workbook getDocType(String filePath) {
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                wb = new XSSFWorkbook(is);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    //格式化单元格格式
    private static Object getCellValue(Cell cell) {
        Object cellValue = null;

        if (cell != null) {
            cell.setCellType(CellType.STRING);
            cellValue = cell.getRichStringCellValue().getString();
        } else {
            cellValue = "";
        }

        return cellValue;
    }

    //导入情况信息
    public static String getMsg(int num) {
        String msg;
        if (num == 0) {
            msg = "导入成功！";
        } else {
            msg = "导入失败！";
        }
        return msg;
    }
}
